package engine.ui.equipStrenGthen 
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.View;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.NewItemChangeUI;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.NxClip;
	import newx.com.control.text.NxLabel;
	/**
	 * ...
	 * @author ...WangHaiBo 
	 * ...........www.woniu.com
	 * 装备打孔
	 */
	public class EquipStilettoLogic implements IEquipLogic 
	{
		private static var _sef			:EquipStilettoLogic;
		//界面
		private var controlSprit		:MovieClip;
		private var _viewGroup			:NxViewGroup;
		private var _pic_0				:MovieClip;
		private var _pic_1				:MovieClip;
		private var _pic_2				:MovieClip;
		private var _pic_3				:MovieClip;
		private var _label_0			:NxLabel
		private var _label_1			:NxLabel
		private var _label_2			:NxLabel
		private var _label_3			:NxLabel
		private var _needMoneyLabel		:NxLabel;
		private var _successNumLabel	:NxLabel;
		private var _starBtn			:NxButton;
		
		private var Jewel0:int;
		private var Jewel1:int;
		private var Jewel2:int;
		private var Jewel3:int;
		private var holeCount:int
		private var _viewData:BaseObject	=	null;
		private var _bindManage:BindManage;
		private var _model:ModelLocator
		public function EquipStilettoLogic(dis:MovieClip) 
		{
			controlSprit	=	dis;
			_bindManage		=	BindManage.getInstance();
			_model			=	ModelLocator.getInstance();
			if (controlSprit == null) return;
			_viewGroup			=	controlSprit.getChildByName("viewgroup_left_1") as NxViewGroup;
			_pic_0				=	controlSprit.getChildByName("hole1_pic_1") as MovieClip;
			_pic_1				=	controlSprit.getChildByName("hole2_pic_1") as MovieClip;
			_pic_2				=	controlSprit.getChildByName("hole3_pic_1") as MovieClip;
			_pic_3				=	controlSprit.getChildByName("hole4_pic_1") as MovieClip;
			_label_0			=	controlSprit.getChildByName("hole1_lab_1") as NxLabel;
			_label_1			=	controlSprit.getChildByName("hole2_lab_1") as NxLabel;
			_label_2			=	controlSprit.getChildByName("hole3_lab_1") as NxLabel;
			_label_3			=	controlSprit.getChildByName("hole4_lab_1") as NxLabel;
			_needMoneyLabel		=	controlSprit.getChildByName("lab_needmoney_1") as NxLabel;
			_successNumLabel	=	controlSprit.getChildByName("lab_succeedpercent_1") as NxLabel;
			_starBtn			=	controlSprit.getChildByName("btn_star_1") as NxButton;
		}
		/**
		 * 界面显示类容，需要同时修改
		 * @param	dis
		 * @return
		 */
		public static function getInstance(dis:MovieClip):EquipStilettoLogic
		{
			if (_sef == null) 
			{
				_sef	=	new EquipStilettoLogic(dis);
			}
			return _sef;
		}
		/**
		 * 添加装备
		 * @param	val
		 */
		public function addEquip(val:BaseObject):void
		{
			var item:MovieClip	=	_viewGroup.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			_viewData				=	val;
			var equipClip:NxClip	=	new NxClip();
			equipClip.width			=	equipClip.height	=	40;
			equipClip.x				=	6;
			equipClip.y				=	5;
			val.HandleProperties("NowViewType",View.VIEWPORT_ADDHOLE)
			item["data"]			=	val;
			equipClip.source		=	"res/items/icon_" + val.GetProperties("Photo") + ".jpg";
			item.addChild(equipClip);
			_starBtn.enabled		=	true;
			needMenoy();
			succeedPercent();
			addBind();
			addListent();
		}
		/**
		 * 移除装备
		 */
		public function removeEquip(val:BaseObject):void
		{

			var item:MovieClip	=	_viewGroup.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			removeBind();
			item["data"]			=	null;
			_viewData				=	null;
			reduction();
			removeListent()
		}
		/**
		 * 清除所有显示对象信息
		 */
		public function removeAll():void
		{
			var item:MovieClip	=	_viewGroup.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			removeBind();
			item["data"]			=	null;
			_viewData				=	null;
			reduction();
		}
		/**
		 * 还原各显示对象
		 */
		private function reduction():void
		{
			_pic_0.visible			=	false;
			_pic_1.visible			=	false;
			_pic_2.visible			=	false;
			_pic_3.visible			=	false;
			_label_0.text			=	"";
			_label_1.text			=	"";
			_label_2.text			=	"";
			_label_3.text			=	"";
			_needMoneyLabel.htmlText	=	"";
			_successNumLabel.htmlText	=	"";
			_starBtn.enabled			=	false;
		}
		/**
		 * 添加绑定
		 */
		private function addBind():void
		{
			removeBind();
			Jewel0		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel0", 	this, "fristLabelData");
			Jewel1		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel1", 	this, "secondLabelData");
			Jewel2		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel2", 	this, "threeLabelData");
			Jewel3		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel3", 	this, "fourLabelData");
			holeCount	=	_bindManage.BindProp(_viewData as ObjectData, "HoleCount", 	this, "holeCountlData");
		}
		/**
		 * 删除绑定
		 */
		private function removeBind():void
		{
			if (_viewData == null) return;
			_bindManage.UnbindProp(_viewData as ObjectData, "fristLabelData", 	Jewel0);
			_bindManage.UnbindProp(_viewData as ObjectData, "secondLabelData", Jewel1);
			_bindManage.UnbindProp(_viewData as ObjectData, "threeLabelData", 	Jewel2);
			_bindManage.UnbindProp(_viewData as ObjectData, "fourLabelData", 	Jewel3);
			_bindManage.UnbindProp(_viewData as ObjectData, "holeCountlData",	holeCount);
			
		}
		/**
		 * 孔洞一
		 */
		public function set fristLabelData(val:String):void
		{
			showEquipProperty(val, 0, _label_0, _pic_0);
		}
		/**
		 * 孔洞二
		 */
		public function set secondLabelData(val:String):void
		{
			showEquipProperty(val, 1, _label_1, _pic_1);
		}
		/**
		 * 孔洞三
		 */
		public function set threeLabelData(val:String):void
		{
			showEquipProperty(val, 2, _label_2, _pic_2);
		}
		/**
		 * 孔洞四
		 */
		public function set fourLabelData(val:String):void
		{
			showEquipProperty(val, 3, _label_3, _pic_3);
		}
		/**
		 * 孔洞数
		 */
		public function set holeCountlData(val:String):void
		{
			if (int(val) < 4) 
			{
				_starBtn.enabled			=	true;
				needMenoy();
			}
			else
			{
				_needMoneyLabel.htmlText	=	"";
				_successNumLabel.htmlText	=	"";
				_starBtn.enabled			=	false;
			}
			
		}
		/**
		 * 显示装备的特殊属性
		 * @param	holeVal_
		 * @param	holeNum_
		 * @param	label_
		 * @param	pic_
		 */
		private function showEquipProperty(holeVal_:String,holeNum_:int,label_:NxLabel,pic_:MovieClip):void
		{
			var eqType:String	=	"";

			
			if (holeVal_ == "-1") 
			{
				label_.color			=	0x00FF00
				eqType					=	StaticIni.getSwfLangStr("F_Gem_13234")
				pic_.visible			=	true;
			}
			else if (holeVal_ == "0") 
			{
				label_.color			=	0xFF0000
				eqType					=	StaticIni.getSwfLangStr("F_Gem_13245")
				pic_.visible			=	false;
			}
			else
			{	
				if (_viewData != null)
				{
					label_.color		=	0xFFFF00
					eqType				=	NewItemChangeUI.getInstance().readSpecialProperty(holeNum_,_viewData as ObjectData);
					pic_.visible		=	true;
				}
			}
			label_.text	=	eqType;
		}
		/**
		 * 打孔所要的金钱
		 * @param	val
		 */
		private function needMenoy():void
		{
			var tempMoney:int 	=	0;
			var menoyStr:String	=	"";
			var needLevel:int
			var tempInt:int	
			if (_viewData != null)
			{
				if(_viewData.CheckProperties("NeedLevel"))
				{
					needLevel		=	_viewData.GetProperties("NeedLevel")
					tempInt			=	int(_viewData.GetProperties("HoleCount"));
					if (tempInt <3) 
					{
						tempMoney	=	needLevel * 200 + 2000;
					}
					else
					{
						tempMoney	=	3*(needLevel * 200 + 2000);
					}
				}
			}
			menoyStr					=	tempMoney == 0	?	""	:	String(tempMoney);
			
			if (tempMoney <= _model.scene.Role.getGoldCoin()) 
			{
				menoyStr	=	"<font color='#00FF40'>"	+	menoyStr	+	"</font>"
			}
			else
			{
				menoyStr	=	"<font color='#FF0000'>"	+	menoyStr	+	"</font>"
			}
			_needMoneyLabel.htmlText		=	StaticIni.getSwfLangStrVar("F_Gem_13222", [menoyStr])	+	StaticIni.getSwfLangStr("CapitalType_0");
		}
		/**
		 * 成功率
		 */
		private function succeedPercent():void
		{
			var succeedStr:String	=	"";
			if (_viewData == null) 
			{
				succeedStr	=	"";
			}
			else
			{
				succeedStr	=	"<font color='#00FF40'>100%</font>"
			}
			_successNumLabel.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_36", [succeedStr]);
		}
		/**
		 * 开始打孔
		 * @param	e
		 */
		private function starBtnClickHandle(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDHOLE);
		}
		/**
		 * 添加事件
		 */
		private function addListent():void
		{
			_starBtn.addEventListener(MouseEvent.CLICK,		starBtnClickHandle);
		}
		/**
		 * 删除事件
		 */
		private function removeListent():void
		{
			_starBtn.removeEventListener(MouseEvent.CLICK,		starBtnClickHandle);
		}
		
	}

}